package com.idbk.chargestation.activity.user;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.idbk.chargestation.AppContext;
import com.idbk.chargestation.R;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonBase;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.GsonUtils;

/**
 * 我要建桩
 * @author lupc
 *
 */
public class ActivityBuildPile extends BaseActivity implements OnClickListener{

	private Context mContext;

	private EditText mEditName,mEditEmail,mEditPhone,mEditRemark;

		@Override
		protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_get_pile);

		mContext = this;

		setupView();

		setupData();
	}

	private void setupView(){
		setupToolBar2();

		mEditName = (EditText)findViewById(R.id.edittext_gp_name);
		mEditEmail = (EditText)findViewById(R.id.edittext_gp_email);
		mEditPhone = (EditText)findViewById(R.id.edittext_gp_phone);
		mEditRemark = (EditText)findViewById(R.id.edittext_gp_remark);

		findViewById(R.id.textview_submit).setOnClickListener(this);
	}

	private void setupData(){
		AppContext ac = AppContext.getInstance();
		if(ac != null){
			String phone = ac.getUserPhoneNumber();
			if(phone != null){
				mEditPhone.setText(phone);
			}
		}
	}

	private final EHttpResponse mResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonBase result = GsonUtils.toBean(JsonBase.class, response);
			if(handleResponseStatus(result)){
				new AlertDialog.Builder(mContext)
				.setCancelable(false)
				.setMessage("提交成功！")				
				.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				})
				.create()
				.show();
			}
		}

		@Override
		public void onEErrorResponse(com.android.volley.VolleyError error) {
			Snackbar.make(mEditName, "提交失败！", Snackbar.LENGTH_SHORT)
			.setAction("重试", new OnClickListener() {

				@Override
				public void onClick(View v) {
					submit();
				}
			})
			.show();			
		}

		@Override
		public void onEFinish() {
			dismissMyProgressDialog();
		}

	};

	private boolean checkInput(){
		String name = mEditName.getText().toString();
		if(name.equals("") || name.equals("")){
			Snackbar.make(mEditName, "请输入正确的名称！", Snackbar.LENGTH_SHORT).show();
			return false;
		}
		String email = mEditEmail.getText().toString();
		if(email.equals("") || email.equals("")){
			Snackbar.make(mEditName, "请输入正确的邮箱！", Snackbar.LENGTH_SHORT).show();
			return false;
		}
		String phone = mEditPhone.getText().toString();
		if(phone.equals("") || phone.equals("") || phone.length() != 11){
			Snackbar.make(mEditName, "请输入正确的手机号码！", Snackbar.LENGTH_SHORT).show();
			return false;
		}
		String remark = mEditRemark.getText().toString();
		if(remark.equals("") || remark.equals("") || remark.length() > 300){
			Snackbar.make(mEditName, "附加信息不能为空且长度不能超过300！", Snackbar.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	private void submit(){
		if(!checkInput()){
			return;
		}

		String name = mEditName.getText().toString();
		String email = mEditEmail.getText().toString();
		String phone = mEditPhone.getText().toString();
		String remark = mEditRemark.getText().toString();

		showMyProgressDialog(true,true,"提交中...");
		mMainRequest = ChargeStationAPI.buildMyPile(name, email, phone, remark, mResponse);
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.textview_submit){
			submit();
		}
	}

}
